package net.lz1998.pbbot.plugin;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.lz1998.pbbot.constant.BannedConstant;
import net.lz1998.pbbot.utils.HttpRequestUtils;
import onebot.OnebotBase;
import onebot.OnebotEvent;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotContainer;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.constant.TitleConstant;
import net.lz1998.pbbot.mapper.PicsMapper;
import net.lz1998.pbbot.pojo.Pics;
import net.lz1998.pbbot.utils.DateUtils;
import net.lz1998.pbbot.utils.Msg;

/**
 * @ProjectName spring-mirai-server
 * @Description:
 * @Author WT
 * @CreateDate 2020/12/25 18:21
 */

@EnableScheduling
@Component
@RequiredArgsConstructor
public class JobPlugin extends BotPlugin{

	private final PicsMapper picsMapper;

	Map<Long, LocalDateTime> map = new HashMap<>();

	private static boolean flag = false;

	@Autowired
	BotContainer botContainer;

	//每天3 9 15 21 30分钟执行
	@Scheduled(cron = "0 30-30 3,9,15,21 * * ?")
	public void ge4(){
		System.out.println("每天3 9 15 21 30分钟执行");
		List<Pics> pics = picsMapper.findByPics(3, null);
		Msg info = null;
		info = Msg.builder()
				.text("今日小说推荐!")
				.text("\n")
				.text(pics.get(0).getTitle());
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID, info, true);
		bot.sendGroupMsg(TitleConstant.GROUPID, Msg.builder().record(pics.get(0).getImgUrl().replaceAll("\\\\", "/")), true);
	}

	//每天0 6 12 18 点30分钟执行

	@Scheduled(cron = "0 30-30 0,6,12,18 * * ?")
	public void get1(){
		System.out.println("每天0 6 12 18 点30分钟执行");
		String outXiaoHua = VoteKickPlugin.outXiaoHua();
		Bot bot = getBot();
		Msg info = Msg.builder()
				.text("今日笑话!")
				.text("\n\n")
				.text(outXiaoHua);
		bot.sendGroupMsg(TitleConstant.GROUPID, info, true);
	}
	//每天 1 7 13 19点执行
	@Scheduled(cron = "0 0 1,7,13,19 * * ?")
	public void get2(){
		System.out.println("每天 1 7 13 19点执行");
		String outXingZhiShi = VoteKickPlugin.outXingZhiShi();
		Bot bot = getBot();
		Msg info = Msg.builder()
				.text("今日性知识普及!")
				.text("\n\n")
				.text(outXingZhiShi);
		bot.sendGroupMsg(TitleConstant.GROUPID, info, true);
	}
	//每天2 8 14 20点执行
	@Scheduled(cron = "0 0 2,8,14,20 * * ?")
	public void get3(){
		System.out.println("每天2 8 14 20点执行");
		String outNvYou = VoteKickPlugin.outNvYou();
		Bot bot = getBot();
		Msg info = Msg.builder()
				.text("今日女优介绍!")
				.text("\n\n")
				.text(outNvYou);
		bot.sendGroupMsg(TitleConstant.GROUPID, info, true);
	}
	//每天4 10 16 22点执行
	@Scheduled(cron = "0 0 4,10,16,22 * * ?")
	public void get5(){
		Bot bot = getBot();
		List<Pics> pics = picsMapper.findByPics(0, null);
		Msg image = Msg.builder()
				.text("老司机发车了,来不及了，快上车!")
				.text("\n\n")
				.text(pics.get(0).getTitle())
				.text("\n\n")
				.image(pics.get(0).getImgUrl().replaceAll("\\\\", "/"));
		bot.sendGroupMsg(TitleConstant.GROUPID, image, true);
	}
	//每天5 11 17 23点执行
	@Scheduled(cron = "0 0 5,11,17,23 * * ?")
	public void get6(){
		List<Pics> pics = picsMapper.findByPics(1, null);
		Msg info = null;
		int price = new Random().nextInt(3000 - 200 + 1) + 200;
		info = Msg.builder()
				.text("今日妹子推荐,只需"+price+"块就能包夜哦,优惠多多!")
				.text("\n\n")
				.text("编号\t\t\t"+pics.get(0).getId())
				.text("\n\n")
				.text("姓名\t\t\t"+pics.get(0).getTitle())
				.image(pics.get(0).getImgUrl().replaceAll("\\\\", "/"));
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID, info, true);
	}

	//每天0点执行
	@Scheduled(cron = "0 0 0 * * ?")
	public void get7(){
		System.out.println("每天0点执行了");
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID,outInfo(0), true);
	}

	//每天19:30点执行
	@Scheduled(cron = "0 30-30 19 * * ? ")
	public void get14(){
		String info="";
		int i = 0;
		do {
			try {
				info = HttpRequestUtils.doGet("https://api.ixiaowai.cn/tgrj/index.php");
				i++;
			} catch (Exception e) {
			}
		} while (StringUtils.isBlank(info) && i < 10);
		SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Msg msg = Msg.builder().text("舔狗日记").text("\n\n").text("日期:\t\t"+format.format(new Date())).text("\n\n").text(info).text("\n\n");
		System.out.println("每天19:30点执行");
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID,msg, true);
		bot.sendGroupMsg(BannedConstant.GROUPID1, msg, true);
		bot.sendGroupMsg(BannedConstant.GROUPID2, msg, true);
		bot.sendGroupMsg(BannedConstant.GROUPID3, msg, true);
	}
	//每天6点执行
	@Scheduled(cron = "0 0 6 * * ?")
	public void get8(){
		System.out.println("每天6点执行了");
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID,outInfo(6), true);
	}

	//每天9点执行
	@Scheduled(cron = "0 0 9 * * ?")
	public void get11(){
		System.out.println("每天9点执行了");
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID,outInfo(9), true);
	}

	//每天12点执行
	@Scheduled(cron = "0 0 12 * * ?")
	public void get9(){
		System.out.println("每天12点执行了");
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID,outInfo(12), true);
	}

	//每天15点执行
	@Scheduled(cron = "0 0 15 * * ?")
	public void get10(){
		System.out.println("每天15点执行了");
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID,outInfo(15), true);
	}

	//每天18点执行
	@Scheduled(cron = "0 0 18 * * ?")
	public void get12(){
		System.out.println("每天18点执行了");
		Bot bot = getBot();
		bot.sendGroupMsg(TitleConstant.GROUPID,outInfo(18), true);
	}

	@Override
	public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
		String msg = event.getRawMessage().trim();
		long groupId = event.getGroupId();
		long id = event.getUserId();
		if(BannedConstant.ADMINID == id||BannedConstant.ADMINID1 == id){
			if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.TIANGOUON)) {
				flag = true;
				bot.sendGroupMsg(groupId, "舔狗模式开启", true);
				bot.sendGroupMsg(groupId, Msg.builder().face(1), false);
			} else if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.TIANGOUOFF)) {
				flag = false;
				bot.sendGroupMsg(groupId, "舔狗模式关闭", true);
				bot.sendGroupMsg(groupId, Msg.builder().face(1), false);
			}
		}
		return MESSAGE_IGNORE;
	}

	//每1分钟执行
	@Scheduled(cron = "0 0/1 * * * ?")
	public void get13(){
		Bot bot = getBot();
		if(flag){
			System.out.println("每1分钟执行");
			map.forEach((k,v)->{
				long between = ChronoUnit.SECONDS.between(map.get(k), LocalDateTime.now());
				System.out.println("用户:"+k+",时间为:"+between);
				//12分钟检查一次
				if (between > 12*60) {
					System.out.println("给用户:"+k+"发信息:在干嘛呢?");
					bot.sendPrivateMsg(k, "在干嘛呢?", false);
					map.put(k, LocalDateTime.now());
				}
			});
		}
	}

	@Override
	public int onPrivateMessage(@NotNull Bot bot, @NotNull OnebotEvent.PrivateMessageEvent event) {
		long userId = event.getUserId();
		String msg = event.getRawMessage().trim();
		if (map.get(userId) != null) {
			map.remove(userId);
		}
		map.put(userId, LocalDateTime.now());

		if("拜拜".equals(msg)) {
			map.remove(userId);
		}
		return MESSAGE_IGNORE;
	}


	private Bot getBot(){
		if(botContainer.getBots().get(1481062763L)!=null){
			return botContainer.getBots().get(1481062763L);
		}else if(botContainer.getBots().get(577925472L)!=null){
			return botContainer.getBots().get(577925472L);
		}
		return botContainer.getBots().get(1419659624L);
	}

	private List<OnebotBase.Message> outInfo(int hour){
		int day = DateUtils.getDay();
		String info="";
		int num = new Random().nextInt(3)+1;
		switch (day) {
		case 1:
			if(hour==0){
				info="这个点还不睡，明天不上班?";
			}else if(hour==6){
				info="还可以睡几个小时，嗯，睡了..zzz...zzz";
			}else if(hour==9){
				info="周一了,早安，打工人，B话不多说,新的一周又开始了，加油，加油!!!";
			}else if(hour==12){
				info="12点了哦，不去吃饭吗?";
			}else if(hour==15){
				if(num==1){
					info="真是服了，一群LSB";
				}else if (num==2) {
					info="涛哥在做什么啊?出来聊会天..";
				}else{
					info="没啥事做.摸鱼中...";
				}
			}else if(hour==18){
				info="我下班了，你们呢?";
			}
			break;
		case 2:
			if(hour==0){
				info="你在刷剧还是在打游戏?";
			}else if(hour==6){
				info="上个厕所继续睡..zzz...zzz";
			}else if(hour==9){
				info="周二，不想上班的一天..";
			}else if(hour==12){
				info="吃饭不积极，你是要干嘛?";
			}else if(hour==15){
				if(num==1){
					info="LSB们出来开车了";
				}else if (num==2) {
					info="最近有个B老骚扰我，好烦..";
				}else{
					info="妹子约我下班看电影，怎么办?";
				}
			}else if(hour==18){
				info="我去约妹子去了，单身狗们别羡慕!!!";
			}
			break;
		case 3:
			if(hour==0){
				info="你是在做舔狗?还不睡.";
			}else if(hour==6){
				info="今天有点失眠，凌晨三点才睡着..";
			}else if(hour==9){
				info="周三了啊,时间是挺公平，活一天就拥有24小时，差别只是珍惜。你若不相信努力和时光，时光一定第一个辜负你。请告诉自己：立刻行动!!因为现在过的每一天，都是余生中最年轻的一天！";
			}else if(hour==12){
				info="干饭，干饭..";
			}else if(hour==15){
				if(num==1){
					info="工作中，勿扰..";
				}else if (num==2) {
					info="大佬们，给个网站啊，私发我，谢谢..";
				}else{
					info="好困啊，打瞌睡..";
				}
			}else if(hour==18){
				info="下班妹子让我去接她，我先走了..";
			}
			break;
		case 4:
			if(hour==0){
				info="最近有什么好玩的东西啊，睡不着,哎..";
			}else if(hour==6){
				info="6点了吗,我还没睡着，我快疯了..";
			}else if(hour==9){
				info="周四了,该来的都会来，该走的全会走，别抗拒，别挽留，别贪恋。学着看淡一些事情。愿你永远像一个小孩子，每天都充满正能量，开始元气满满的一天。";
			}else if(hour==12){
				info="今天有点困，不想吃了,给我带个饭..";
			}else if(hour==15){
				if(num==1){
					info="今天大佬们在干嘛啊，出来吹个B啊..";
				}else if (num==2) {
					info="晚上不知道吃啥了..";
				}else{
					info="看了看，白酒又涨了，不多说，抄底白酒...";
				}
			}else if(hour==18){
				info="妹子说她今天不开心，我好困啊..我觉得我要死了..";
			}
			break;
		case 5:
			if(hour==0){
				info="今天白酒股票涨了，还不错...抄底白酒.";
			}else if(hour==6){
				info="我今天起得特别早，LSB们还不起床?";
			}else if(hour==9){
				info="今天周五,没啥B话，加油，奥利给，明天就休息，早安，LSB们!";
			}else if(hour==12){
				info="不想说话，白酒永远的神..";
			}else if(hour==15){
				if(num==1){
					info="同事说下班跟我一起走，怎么回事??";
				}else if (num==2) {
					info="同事跟我说她新买了个丁字裤...";
				}else{
					info="同事骚扰我，怎么办啊，不停拿腿踢我..";
				}
			}else if(hour==18){
				info="下班了，夜生活开始了...";
			}
			break;
		case 6:
			if(hour==0){
				info="我在302包厢，有人来吗，妹子多多哦..";
			}else if(hour==6){
				info="昨晚玩到凌晨三点，有点累啊..";
			}else if(hour==9){
				info="周六了啊.不知道你们有没有起床，反正我还在睡觉！";
			}else if(hour==12){
				info="zzz...zzz..zzz.zzz";
			}else if(hour==15){
				if(num==1){
					info="吃鸡，吃鸡...";
				}else if (num==2) {
					info="睡觉去了...";
				}else{
					info="打会游戏，吃鸡去了，有一起的吗???";
				}
			}else if(hour==18){
				info="今晚干嘛呢，继续约妹子??";
			}
			break;
		case 7:
			if(hour==0){
				info="妹子说她家里着火了,不知道现在什么情况了.";
			}else if(hour==6){
				info="今天看来没啥事，接着睡zzz..zzz.zzz";
			}else if(hour==9){
				info="周末早上干嘛呢，股票涨了吗?";
			}else if(hour==12){
				info="打打游戏吧，没啥事干，你们都玩啥呢?";
			}else if(hour==15){
				if(num==1){
					info="兄弟们，给个网站啊..";
				}else if (num==2) {
					info="刚撸完，神清气爽..";
				}else{
					info="突然想撸一发...";
				}
			}else if(hour==18){
				info="这么快天就黑了啊，感觉今天没干啥啊，明天又要上班了..艹艹艹..";
			}
		default:
			break;
		}
		return Msg.builder().text(info).build();
	}
}
